﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MVCClubSite.BLL;
using MVCClubSite.IDAL;
using System.Configuration;
using System.Web.Configuration;
using MVCClubSite.Web.ViewData;
using MVCClubSite.Model;
using System.Web.Security;
using System.Globalization;
using MVCClubSite.Web.Helpers;

namespace MVCClubSite.Web.Controllers
{
    public class AnnouncementsController : BaseController
    {
        AnnouncementsService _announcementService;
        AlbumsService _albumsService;

        public AnnouncementsController()
            : this(null, null)
        {
            _announcementService = new AnnouncementsService();
            _albumsService = new AlbumsService();
        }

        public AnnouncementsController(AnnouncementsService announcementService, AlbumsService albumsService)
        {
            _announcementService = announcementService ?? new AnnouncementsService();
            _albumsService = albumsService ?? new AlbumsService();
        }


        public ActionResult List(int? page)
        {
            AnnouncementListViewData data = new AnnouncementListViewData();
            data.Title = "Announcements";
            int index = page.HasValue ? page.Value - 1 : 0;
            data.Announcements = _announcementService.GetVisibleAnnouncements().ToPagedList(index, Config.Announcements.PageSize);

            return View("List", data);
        }

        [AcceptVerbs("GET")]
        public ActionResult New()
        {
            AnnouncementViewData data = new AnnouncementViewData();
            data.Albums = _albumsService.GetAllAlbums();
            return View("New", data);
        }

        [AcceptVerbs("POST")]
        public ActionResult Create(FormCollection form)
        {
            Announcement newAnnouncement = new Announcement();

            this.UpdateModel(newAnnouncement, new[] { "Title", "Description", "AlbumID"});

            if (String.IsNullOrEmpty(form["Title"]))
                ModelState.AddModelError("title", "You must specify a title.");

            if (String.IsNullOrEmpty(form["Description"]))
                ModelState.AddModelError("description", "You must specify a description.");

            DateTime announcementDate;
            bool isValidDate = DateTime.TryParse(form["AnnouncementDate"], DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out announcementDate);

            if (isValidDate)
                ModelState.AddModelError("announcementDate", "You must enter a valid date.");
            else
                newAnnouncement.AnnouncementDate = announcementDate;


            if (ViewData.ModelState.IsValid)
            {
                newAnnouncement.Author = (Guid)Membership.GetUser().ProviderUserKey;
                _announcementService.Save(newAnnouncement);
                return RedirectToAction("List");
            }

            AnnouncementViewData data = new AnnouncementViewData();
            data.Albums = _albumsService.GetAllAlbums();
            return View("New", data);
        }

        [AcceptVerbs("Get")]
        public ActionResult Details(string id)
        {
            int announcementID = int.Parse(id);
            AnnouncementViewData data = new AnnouncementViewData();
            data.Announcement = _announcementService.GetAnnouncementByID(announcementID);
            
            return View("Details", data);
        }

        [AcceptVerbs("GET")]
        public ActionResult Edit(int id)
        {
            AnnouncementViewData data = new AnnouncementViewData();
            data.Announcement = _announcementService.GetAnnouncementByID(id);
            data.Albums = _albumsService.GetAllAlbums();

            return View("Edit", data);
        }

        [AcceptVerbs("POST")]
        public ActionResult Update(FormCollection form, int id)
        {
            Announcement existingAnnouncement = _announcementService.GetAnnouncementByID(id);

            this.UpdateModel(existingAnnouncement, new[] { "Title", "Description", "AlbumID", "AnnouncementDate" });
            _announcementService.Save(existingAnnouncement);

            return RedirectToAction("List");
        }

        [AcceptVerbs("POST")]
        public ActionResult Delete(int id)
        {
            _announcementService.Delete(id);
            return RedirectToAction("List");
        }

    }
}
